﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Source;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Source;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 资产来源
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/source")]
    public class SourcesController : ApiController
    {
        /// <summary>
        /// 资产来源列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("infos")]
        public async Task<IHttpActionResult> GetStatusList()
        {
            return this.SendData(await SourcesManger.GetList());
        }

        /// <summary>
        /// 添加资产来源
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("infos")]
        public async Task<IHttpActionResult> CreateSources(CreateSourceViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            return await SourcesManger.Create(model.SourceName) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }
        /// <summary>
        /// 获取来源信息
        /// </summary>
        /// <param name="sourcesid">来源Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{sourcesid}/infos")]
        public async Task<IHttpActionResult> GetSources(Guid sourcesid) {
           return this.SendData(await SourcesManger.GetById(sourcesid));
        }

        /// <summary>
        /// 更新来源名称
        /// </summary>
        /// <param name="sourcesid">来源Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{sourcesid}/infos")]
        public async Task<IHttpActionResult> UpdateStatus(Guid sourcesid, SourceModel model)
        {
            model.Id = sourcesid;
            return await SourcesManger.Update(model) ? this.SendData("修改成功") : this.ErrorData("修改失败");
        }

        /// <summary>
        /// 删除资产来源
        /// </summary>
        /// <param name="sourcesid">来源Id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{sourcesid}/infos")]        
        public async Task<IHttpActionResult> DeleteSources(Guid sourcesid) {
            return await SourcesManger.Delete(sourcesid) ? this.SendData("删除成功") : this.ErrorData("删除失败");
        }

    }
}
